package butines.game.lastcolony.entities;

import java.awt.Graphics2D;

import butines.core.GameImage;


public class Terrain extends Entity {

	public static final EntityClass OILFIELD;
	public static final EntityClass BIG_ROCKS;
	public static final EntityClass SMALL_ROCKS;

	static {
		OILFIELD = new EntityClass();
		OILFIELD.setSelectable(false);
		OILFIELD.setAction(EntityAction.DEFAULT);
		OILFIELD.setName("Oilfield");
		OILFIELD.setPixelWidth(40);
		OILFIELD.setPixelHeight(60);
		OILFIELD.setBaseWidth(40);
		OILFIELD.setBaseHeight(20);
		OILFIELD.setPixelOffsetX(0);
		OILFIELD.setPixelOffsetY(40);
		OILFIELD.setBuildableGrid(new int[][] {{1, 1}});
		OILFIELD.setPassableGrid(new int[][] {{1, 1}});
		OILFIELD.setSpriteSheet(new GameImage("images/terrain/oilfield.png"));
		OILFIELD.addEntitySprite("hint", 1);
		OILFIELD.addEntitySprite("default", 1);
		
		BIG_ROCKS = new EntityClass();
		BIG_ROCKS.setSelectable(false);
		BIG_ROCKS.setAction(EntityAction.DEFAULT);
		BIG_ROCKS.setName("Big rocks");
		BIG_ROCKS.setPixelWidth(40);
		BIG_ROCKS.setPixelHeight(70);
		BIG_ROCKS.setBaseWidth(40);
		BIG_ROCKS.setBaseHeight(40);
		BIG_ROCKS.setPixelOffsetX(0);
		BIG_ROCKS.setPixelOffsetY(30);
		BIG_ROCKS.setBuildableGrid(new int[][] {{1, 1}, {0, 1}});
		BIG_ROCKS.setPassableGrid(new int[][] {{1, 1}, {0, 1}});
		BIG_ROCKS.setSpriteSheet(new GameImage("images/terrain/bigrocks.png"));
		BIG_ROCKS.addEntitySprite("default", 1);
		
		SMALL_ROCKS = new EntityClass();
		SMALL_ROCKS.setSelectable(false);
		SMALL_ROCKS.setAction(EntityAction.DEFAULT);
		SMALL_ROCKS.setName("Small rocks");
		SMALL_ROCKS.setPixelWidth(20);
		SMALL_ROCKS.setPixelHeight(35);
		SMALL_ROCKS.setBaseWidth(20);
		SMALL_ROCKS.setBaseHeight(20);
		SMALL_ROCKS.setPixelOffsetX(0);
		SMALL_ROCKS.setPixelOffsetY(15);
		SMALL_ROCKS.setBuildableGrid(new int[][] {{1}});
		SMALL_ROCKS.setPassableGrid(new int[][] {{1}});
		SMALL_ROCKS.setSpriteSheet(new GameImage("images/terrain/smallrocks.png"));
		SMALL_ROCKS.addEntitySprite("default", 1);
	}
	
    public static Terrain create(String name, Object ... values) {
        if ("oilfield".equals(name)) {
        	return OILFIELD.create(Terrain.class, values);
        } else if ("bigrocks".equals(name)) {
        	return BIG_ROCKS.create(Terrain.class, values);
        } else if ("smallrocks".equals(name)) {
        	return SMALL_ROCKS.create(Terrain.class, values);
        }
        return null;
    }
	
	public Terrain() {
		
	}

	public void animate() {
		if (action == EntityAction.DEFAULT) {
			imageList = getEntityClass().getEntitySprite("default");
			imageOffset = imageList.getOffset() + animationIndex;
			animationIndex++;
			if (animationIndex >= imageList.getCount()){
				animationIndex = 0;
			}
		}
		if (getAction() == EntityAction.HINT) {
			imageList = entityClass.getEntitySprite("hint");
			imageOffset = imageList.getOffset() + animationIndex;
			animationIndex++;
			if (animationIndex >= imageList.getCount()){
				animationIndex = 0;
			}
		}
	}

	@Override
	public void draw(Graphics2D g2d) {
		
		float sx = (x * game.getGridSize()) - game.getOffsetX() - entityClass.getPixelOffsetX();
        float sy = (y * game.getGridSize()) - game.getOffsetY() - entityClass.getPixelOffsetY();
       
        int ix = imageOffset * entityClass.getPixelWidth();
        int iy = 0;
        int iw = entityClass.getPixelWidth();
        int ih = entityClass.getPixelHeight();
	        
        getEntityClass().getSpriteSheet().draw(g2d, (int) sx, (int) sy, ix, iy, iw, ih);
	}
	
    public void processOrders() {
        
    }
	
}
